import { batchModifyDepartApi } from "@/api/managementCenter/departmentPersonnel";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formDataRef, formState, emit, passData } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });
  const onFinish = () => {
    const deptIds = [];
    for (let i = 0; i < formState.deptId.length; i++) {
      const target = formState.deptId[i];
      if (Object.prototype.toString.call(target) === "[object Object]") deptIds.push(target.value);
      else deptIds.push(target);
    }
    const params = {
      userIdList: passData.value.map((t: DepartmentPersonnel.ListRows) => t.id),
      deptId: deptIds.join(",")
    };
    batchModifyDepartApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("操作成功!");
        emit("submit");
      }
    });
  };
  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    resetFields,
    validateZhInputHandle
  };
}
